package com.caicai.ssm.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.caicai.ssm.entity.Goods;
import com.caicai.ssm.entity.User;
import com.caicai.ssm.service.impl.LoginServiceImpl;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "loginController")
public class LoginController {

	@Resource
	private LoginServiceImpl service;

	/**
	 * 判断用户登陆(使用的是转发)
	 * 
	 * @param username
	 *            用户输入的用户名
	 * @param password
	 *            用户输入的密码
	 * @return 返回逻辑视图名称
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		ModelAndView mv = service.isLogin(username, password);
		PageInfo<Goods> pageInfo = service.findAllGoodsPage(pageNum, 10);
		List<Goods> list = pageInfo.getList();
		mv.addObject("list", list);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	/**
	 * 前后缀的使用限制: 不能处理带有forward|redirec前缀的服务方法返回值. 前后缀只能处理请求转发,不能处理响应重定向的字符串返回结果.
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login2.do", method = RequestMethod.POST)
	public String login2(@RequestParam("username") String username, @RequestParam("password") String password) {
		// String path=service.isLogin(username, password);
		// request.addObject("username", username);
		// mv.setViewName("redirect:"+path);//设置逻辑视图
		return "redirect:/views/test.jsp";
	}

	/**
	 * 进行分页查询,
	 * 
	 * @param username
	 * @param password
	 * @param pageNum
	 * @return
	 */
	@RequestMapping(value = "/login3.do")
	public ModelAndView login3(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		ModelAndView mv = new ModelAndView();
		PageInfo<Goods> pageInfo = service.findAllGoodsPage(pageNum, 10);
		List<Goods> list = pageInfo.getList();
		mv.addObject("list", list);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("/success");
		return mv;
	}

	/**
	 * 文件上传
	 * 
	 * @param file
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/upload.do")
	@ResponseBody
	public User upload(MultipartFile file, HttpServletRequest request) throws IOException {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename(); // 得到文件原始名称
		File dir = new File(path, fileName); //
		if (!dir.exists()) {
			dir.mkdirs();
		}
		file.transferTo(dir); // 上传文件
		User user = new User("admin","123",1);
		return user;
	}
	/**
	 * 文件下载
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/download.do")
	public void down(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 模拟文件，myfile.txt为需要下载的文件
		String fileName = request.getSession().getServletContext().getRealPath("upload") + "/1.jpg";
		// 获取输入流
		InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
		// 假如以中文名下载的话
		String filename = "下载文件.txt";
		// 转码，免得文件名中文乱码
		filename = URLEncoder.encode(filename, "UTF-8");
		// 设置文件下载头
		response.addHeader("Content-Disposition", "attachment;filename=" + filename);
		// 1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
		response.setContentType("multipart/form-data");
		BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
		int len = 0;
		while ((len = bis.read()) != -1) {
			out.write(len);
			out.flush();
		}
		out.close();
	}

}
